/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.baumi.tankwars.entities;

import it.baumi.tankwars.Game;
import it.baumi.tankwars.util.Timer;
import java.util.ArrayList;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Michael Baumgartner
 */
public class TankEntity extends MoveableEntity2D {

    protected Game game;

    protected boolean firing;

    protected ArrayList<ShotEntity> shots;
    protected ArrayList<ShotEntity> removeShots;

    protected final int MAX_SHOTS_COUNT = 5;
    protected final double SHOOT_SPEED = 3.5f;
    protected final double SHOOT_RATE = 200;
    protected final double SHOT_HEIGHT = 6;
    protected final double SHOT_WIDTH = 12;

    protected long lastShootTime;

    public TankEntity(Game game, float x, float y, float width, float height, float speed) {
        super(x, y, width, height, speed);
        this.game = game;
        firing = false;
        shots = new ArrayList<ShotEntity>();
        removeShots = new ArrayList<ShotEntity>();
        lastShootTime = 0;

    }

    public ArrayList<ShotEntity> getShots() {
        return shots;
    }

    public boolean isFiring() {
        return firing;
    }

    public void setFiring(boolean firing) {
        this.firing = firing;
    }

    @Override
    public void draw() {

        GL11.glPushMatrix();
        GL11.glLoadIdentity();

        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2d(x - width / 2, y - height / 2);
        GL11.glVertex2d(x - width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y - height / 2);

        GL11.glEnd();

        //panzerrohr
        float targetX = Mouse.getX();
        //orthogonalSystem startet in der linken oberen Ecke, deshalb muss der mouse y wert umgerechnet werden
        float targetY = Display.getHeight() - Mouse.getY();
        //mouse koordinaten in richtung des Ursprungs verschieben um richtungs vektor zu bekommen
        double dx = targetX - x;
        double dy = targetY - y;
        //distanz zwischen mousezeiger und panzer mittelpunkt berechnen
        double vektorBetrag = Math.sqrt(dx * dx + dy * dy);
        //Ortsvektor durch länge teilen und auf gewünschte länge anpassen und zum mittelpunkt des panzers verschieben
        double size = width > height ? width : height;
        double endpunktRohrX = x + dx / vektorBetrag * size * 1.1f;
        double endPunktRohrY = y + dy / vektorBetrag * size * 1.1f;
        GL11.glBegin(GL11.GL_LINES);
        GL11.glColor3f(0.0f, 1.0f, 1.0f);
        GL11.glVertex2d(x, y);
        GL11.glVertex2d(endpunktRohrX, endPunktRohrY);
        GL11.glEnd();

        //Shots
        if (isFiring()) {
            if ((Timer.getTime() - lastShootTime) > SHOOT_RATE) {
                if (shots.size() < MAX_SHOTS_COUNT) {
                    double degree = Math.atan2(dy, dx);
                    shots.add(new ShotEntity(game, endpunktRohrX, endPunktRohrY, SHOT_WIDTH, SHOT_HEIGHT, SHOOT_SPEED, degree));
                    lastShootTime = Timer.getTime();
                }
            }
        }
        GL11.glPopMatrix();

        GL11.glPushMatrix();

        for (ShotEntity shot : shots) {
            if (shot.isDestroy()) {
                removeShots.add(shot);
            } else {
                shot.draw();
            }
        }

        for (ShotEntity removeShot : removeShots) {
            this.shots.remove(removeShot);
        }
        removeShots.clear();

        GL11.glPopMatrix();
    }

    @Override
    public void setUp() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void collideWith(BlockEntity2D otherEntity) {
//        collision mit wand oder anderem panzer 
        if (otherEntity instanceof WallEntity || otherEntity instanceof TankEntity) {

            double north = this.getyMax() - otherEntity.getyMin();
            double east = otherEntity.getxMax() - this.getxMin();
            double south = otherEntity.getyMax() - this.getyMin();
            double west = this.getxMax() - otherEntity.getxMin();

            double min = Math.min(Math.min(Math.min(north, east), west), south);
            if (min == north) {
                this.y = otherEntity.getyMin() - height / 2 - 0.5f;
            } else if (min == east) {
                this.x = otherEntity.getxMax() + width / 2 + 0.5f;
            } else if (min == south) {
                this.y = otherEntity.getyMax() + height / 2 + 0.5f;
            } else if (min == west) {
                this.x = otherEntity.getxMin() - width / 2 - 0.5f;
            }
        }
    }

    @Override
    public void update() {
        //Panzer vs Wall
        ArrayList<WallEntity> walls = game.getWallList();
        for (WallEntity wall : walls) {
            if (this.intersects(wall)) {
                this.collideWith(wall);
            }
        }
        draw();
    }

}
